home *** CD-ROM | disk | FTP | other *** search
- Path: news.uni-jena.de!news
- From: mkt@isun04.inf.uni-jena.de (Tilo Koerbs)
- Newsgroups: comp.lang.c++
- Subject: Re: delete[] and template questions
- Date: 31 Jan 1996 14:35:05 GMT
- Organization: Lehrstuhl fuer Rechnerarchitektur- und kommunikation, FSU Jena
- Message-ID: <4enump$qlr@fsuj01.rz.uni-jena.de>
- References: <1996Jan30.165654.2033@iiasa.ac.at>
- Reply-To: mkt@isun04.inf.uni-jena.de
- NNTP-Posting-Host: isun07.inf.uni-jena.de
-
- > template <class I, class T>
- > void mVect<I,T>::resize(I new_size) {
- > T *old = v;
- > v = new T[new_size]; // T *v is a private member of mVect
- > int size_of_elem = sizeof(T); // <--- question 3
- > delete[] old; // <--- questions 1 & 2
- > }
- >
- > I have the following questions:
- > 1. Is it absolutely robust and portable to delete[] old, even
- > if a previous call was for new_size == 0 (or if v was allocated
- > by the ctor for the size == 0) ?
- > 2. Is it correct to assume that no destructor is called by this statement ?
- > 3. Is there any risk involved in using sizeof(T) in this statement ?
-
- 1. If you really called new T[x] regardless if x == 0,
- or if old is a NULL pointer
- than the answer is YES!
- 2. For every object created by the corresponding 'new' the destructor
- is called there. (For x = new T[10]; on delete [] x; 10 constructors
- will be called.)
- If zero objects were created (new T[0]) so no destructor is called!
- 3. NO!
- There is no risk at all. When the template is intantiated
- the size of T is known! Otherwise the statement new T[..]
- cannot be compiled!
-
-
-